/*
 *	common.c
 *	written by Holmes Futrell
 *	use however you want
 */

#include "common.h"

int powerOfTwo(int p) {
	int val = 1;
	while (val < p)
		val *= 2;
	return val;
}
/*
	Produces a random int x, min <= x <= max 
	following a uniform distribution
*/
int randomInt(int min, int max) {	
	return min + rand() % (max - min + 1);
}
/*
	Produces a random float x, min <= x <= max 
	following a uniform distribution
 */
float randomFloat(float min, float max) {
	return rand() / (float)RAND_MAX * (max - min) + min;
}


int clampi(int x, int min, int max) {
	if (x < min) return min;
	else if (x > max) return max;
	else return x;
}

float clampf(float x, float min, float max) {
	if (x < min) return min;
	else if (x > max) return max;
	else return x;
}

float signf(float x) {
	if ( x > 0.0 ) return 1.0f;
	else if (x < 0.0) return -1.0f;
	else return 0.0f;
}

float toDegrees(float angle) {
	return angle * 180.0f / pi;
}

void swapf(float *x1, float *x2) {
	float temp = *x1;
	*x1 = *x2;
	*x2 = temp;
}